package com.by.service.impl;

import com.by.bean.StockBean;
import com.by.common.dao.MyBaseServiceImpl;
import com.by.core.utils.Assert;
import com.by.entity.*;
import com.by.mapper.*;
import com.by.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.by.core.utils.Assert.isFail;

/**
 * @author ServivE
 * @version 0.1
 * @className SpuServiceImpl
 * @description
 * @date 2024/11/11 15:16
 * @since jdk17
 */
@Service
public class SpuServiceImpl extends MyBaseServiceImpl<Spu, SpuMapper> implements SpuService {
    @Autowired
    SpuDetailMapper spuDetailMapper; //商品详情

    @Autowired
    SpuExtensionMapper spuExtensionMapper;//商品扩展

    @Autowired
    SkuMapper skuMapper;//

    @Autowired
    SkuStockMapper skuStockMapper;

    @Autowired
    AttrMapper attrMapper;

    @Autowired
    AttrValueMapper attrValueMapper;

    @Autowired
    SpuAttrValueMapper spuAttrValueMapper;

    @Autowired
    SpuSkuAttrValueMapper spuSkuAttrValueMapper;

    @Autowired
    SkuStockLockMapper skuStockLockMapper;


    @Override
    @Transactional
    public int saveEntity(Spu entity) {

        int rs;
        //保存产品信息
         rs= super.saveEntity(entity);
         isFail(rs,"保存spu信息失败");

        //保存产品详情
        SpuDetail spuDetail = new SpuDetail();
        spuDetail.setSpu_id(entity.getSpu_id());
        spuDetail.setDetail(entity.getDetail());
        rs = spuDetailMapper.insert(spuDetail);
        isFail(rs,"保存产品详情失败");

        //保存spu扩展
        int total = 0;//总库存量
        if (null != entity.getSkuList() && entity.getSkuList().size() > 0) {
            Iterator<Sku> it = entity.getSkuList().iterator();
            while (it.hasNext()) {
                Sku sku = it.next();
                total += sku.getActual_stock();//各sku库存量累加
                //保存sku
                //设置产品id
                sku.setSpu_id(entity.getSpu_id());
                rs = skuMapper.insert(sku);
                isFail(rs, "保存sku失败");
                //保存sku库存
                SkuStock skuStock = new SkuStock();
                skuStock.setActual_stock(sku.getActual_stock());
                skuStock.setStock(sku.getActual_stock());
                skuStock.setSku_id(sku.getSku_id());
                rs = skuStockMapper.insert(skuStock);
                isFail(rs, "保存sku库存失败");
            }
        }
        SpuExtension spuExtension = new SpuExtension();
        spuExtension.setSpu_id(entity.getSpu_id());
        spuExtension.setActual_stock(total);
        spuExtension.setStock(total);
        rs = spuExtensionMapper.insert(spuExtension);
        isFail(rs, "保存商品扩展失败");


        //保存规格属性
        if (null != entity.getAttrList() && entity.getAttrList().size() > 0) {
            //遍历规格属性
            entity.getAttrList().forEach(el->{
                //保存
                int tmp = attrMapper.insert(el);
                isFail(tmp, "保存规格属性失败");
                //保存规格值  批量保存
                if (null != el.getAttrValueList() && el.getAttrValueList().size() > 0) {
                    tmp = attrValueMapper.batchSave(el.getAttr_id(), el.getAttrValueList());
                    isFail(tmp, "保存规格属性值失败");
                }

                //保存商品和规格属性  属性值之间的关系
                tmp = spuAttrValueMapper.save(entity.getSpu_id(), el.getAttr_id());
                isFail(tmp, "保存商品.规格.属性值.关系失败");

                //保存 商品 规格 sku关系
                tmp = spuSkuAttrValueMapper.save(entity.getSpu_id());
                isFail(tmp, "保存 商品 规格属性值 sku关系失败");
            });
        }
        return 1;
    }

    @Override
    public Map getDetail(Serializable id) {
        return baseMapper.getDetail(id);
    }

    @Override
    public int upadteStock(List<StockBean> beanList) {

        if (null == beanList || beanList.size() == 0)
            return 0;
        //遍历
        beanList.forEach(bean -> {
                    //更新当前库存
                    int rs = skuStockMapper.updateStock(bean);
                    Assert.isFail(rs, "更新sku_id=" + bean.getSku_id() + "的数量减少" + bean.getCount() + "失败");
                    //更新sku 锁定库存记录
                    rs=skuStockLockMapper.save(bean);
                    //更新spu库存
                    rs = spuExtensionMapper.updateStock(bean.getSpu_id(), bean.getCount());
                    Assert.isFail(rs, "更新spu_id=" + bean.getSpu_id() + "的数量减少" + bean.getCount() + "失败");
                }
        );
        return 1;
    }
}
